package com.chw.admin.order;
 
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chw.utils.CommonFieldsUtil;
import com.chw.utils.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ChwOrderService implements IChwOrderService {

	@Autowired
	private ChwPoHeaderMapper chwPoHeaderMapper;

	@Autowired
	private ChwPoLineMapper chwPoLineMapper;

	// 新增
	@Override
	public int addEntity(ChwPoHeader chwPoHeader, List<ChwPoLine> lineList) {
		ChwPoHeader tmpChwPoHeader = new ChwPoHeader();
		tmpChwPoHeader.setSegment1(chwPoHeader.getSegment1());
		List<ChwPoHeader> list = chwPoHeaderMapper.findByEntity(tmpChwPoHeader);
		if (null != list && list.size() > 0) {
			// 已经存在
			return -1;
		}
		try {
			String status = chwPoHeader.getStatus();
			String poHeaderId = UUIDUtil.getUUID();
			chwPoHeader.setPoHeaderId(poHeaderId);
			CommonFieldsUtil.setCreateValueByCy(chwPoHeader);
			chwPoHeader.setStatus(status);
			this.chwPoHeaderMapper.insert(chwPoHeader);
			if (null != lineList) {
				if (lineList.size() > 0) {
					for (int i = 0; i < lineList.size(); i++) {
						ChwPoLine chwPoLine = lineList.get(i);
						chwPoLine.setPoHeaderId(poHeaderId);
						chwPoLine.setPoLineId(UUIDUtil.getUUID());
						CommonFieldsUtil.setCreateValueByCy(chwPoLine);
						this.chwPoLineMapper.insert(chwPoLine);
					}
				}
			}
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	// 修改
	@Override
	public int updateEntity(ChwPoHeader chwPoHeader, List<ChwPoLine> lineList) {
		try {
			// 更新头
			// chwPoHeader.setLastUpdatedDate(new Date());
			// chwPoHeader.setEnabledFlag("Y");
			CommonFieldsUtil.setUpdateValue(chwPoHeader);
			this.chwPoHeaderMapper.updateByPrimaryKeySelective(chwPoHeader);
			if (null != lineList) {
				if (lineList.size() > 0) {
					String poHeaderId = chwPoHeader.getPoHeaderId();
					// ChwOrder dbChwOrder = new ChwOrder();
					// dbChwOrder.setChwPoHeader(chwPoHeaderMapper.selectByPrimaryKey(poHeaderId));
					// dbChwOrder.setLineList(chwPoLineMapper.findChwPoLineByHeaderId(poHeaderId));
					// 先物理删除行
					 chwPoLineMapper.deleteByHeaderId(poHeaderId);
					// 界面行
					for (int i = 0; i < lineList.size(); i++) {
						// 界面行
						ChwPoLine chwPoLine = lineList.get(i);
						String status = chwPoLine.getStatus();
						String poLineId = chwPoLine.getPoLineId();
						chwPoLine.setPoHeaderId(poHeaderId);
						if (null == poLineId || "".equals(poLineId)) {
							// 新增
							chwPoLine.setPoLineId(UUIDUtil.getUUID());
						} 

						CommonFieldsUtil.setCreateValueByCy(chwPoLine);
						chwPoLine.setStatus(status);
						this.chwPoLineMapper.insert(chwPoLine);
//						else {
//							// 更新
//							// chwPoLine.setLastUpdatedDate(new Date());
//							CommonFieldsUtil.setUpdateValue(chwPoLine);
//							this.chwPoLineMapper.updateByPrimaryKeySelective(chwPoLine);
//						}
					}
				}
			}
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	// 逻辑删除头
	@Override
	public int deleteEntity(String id) {
		try {
			// 失效头
			ChwPoHeader chwPoHeader = this.chwPoHeaderMapper.selectByPrimaryKey(id);
			chwPoHeader.setEnabledFlag("N");
			this.chwPoHeaderMapper.updateByPrimaryKeySelective(chwPoHeader);
			// 失效行
			this.chwPoLineMapper.deleteLineByHeaderId(id);
			return 0;
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return 1;
	}

	// 查询 id
	@Override
	public ChwOrder getEntityById(String id) {
		ChwPoHeader chwPoHeader = this.chwPoHeaderMapper.selectByPrimaryKey(id);
		ChwOrder chwOrder = new ChwOrder();
		chwOrder.setChwPoHeader(chwPoHeader);
		List<ChwPoLine> lineList = this.chwPoLineMapper.findChwPoLineByHeaderId(id); 
		if (lineList.size() > 0) {
			chwOrder.setLineList(lineList);
		}
		return chwOrder;
	}

	// 分页查询
	@Override
	public PageInfo<ChwPoHeader> findEntity(ChwPoHeader chwPoHeader, Integer currentPage, Integer pageSize) {
		PageHelper.startPage(currentPage.intValue(), pageSize.intValue());
		List<ChwPoHeader> list = this.chwPoHeaderMapper.findByEntity(chwPoHeader);
		return new PageInfo(list);
	}

	// 分页查询
	@Override
	public PageInfo<ChwPoLine> findChwPoLine(ChwPoLine chwPoLine, Integer currentPage, Integer pageSize) {
		PageHelper.startPage(currentPage.intValue(), pageSize.intValue());
		List<ChwPoLine> list = this.chwPoLineMapper.findChwPoLineByEntity(chwPoLine);
		return new PageInfo(list);
	}
}